package cc.fozone.note.view;

import javafx.beans.value.ChangeListener;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import cc.fozone.note.configuration.IConfiguration;
import cc.fozone.note.view.handler.UpdateScrollHeightHandler;

public class IndexView implements IView {
	private IConfiguration<String,String> configuration;
	private Stage stage;
	private Scene scene;
	private Parent root;
	private NavigatorView navigator;
	private ContentView content;
	
	private double width = 1000;
	private double height = 640;
	private boolean resizable = false;
	private String style = "app.css";
	private String styleClass = "index";
	public IndexView() {
		super();
		// TODO Auto-generated constructor stub
	}

	public IndexView(Stage stage, IConfiguration<String,String> configuration) {
		this.stage = stage;
		this.configuration = configuration;
	}
	
	public Stage getStage() {
		return stage;
	}

	public void setStage(Stage stage) {
		this.stage = stage;
	}
	
	public NavigatorView getNavigator(){
		return this.navigator;
	}
	
	public ContentView getContent(){
		return this.content;
	}

	public IConfiguration<String, String> getConfiguration() {
		return configuration;
	}

	public void setConfiguration(IConfiguration<String, String> configuration) {
		this.configuration = configuration;
	}

	public void init() {
		this.buildConfiguration();
		this.buildView();
		this.buildLayout();
	}
	
	private void buildConfiguration(){
		width = Double.parseDouble(configuration.get("app.width", "1000"));
		height = Double.parseDouble(configuration.get("app.height","600"));
		
		resizable = Boolean.parseBoolean(configuration.get("app.resizable","false"));
		style = configuration.get("app.style", "app.css");
		
		styleClass = configuration.get("app.index.styleClass",styleClass);
		
		stage.setTitle(configuration.get("app.name"));
	}
	
	private void buildView(){
		BorderPane pane = new BorderPane();
		
		navigator = new NavigatorView(this,configuration);
		IView navigatorView = (IView) navigator;
		navigatorView.init();
		navigatorView.handle();
		
		pane.setTop(navigator);
		
		content = new ContentView(this,configuration);
		IView contentView = (IView) content;
		contentView.init();
		contentView.handle();
		pane.setCenter(content);
		
		this.root = pane;
		this.root.getStyleClass().add(styleClass);
	}
	
	private void buildLayout(){
		scene = new Scene(root,width,height);
		scene.getStylesheets().add(style);
		stage.setScene(scene);
		stage.sizeToScene();
		stage.centerOnScreen();
		stage.setResizable(resizable);
	}
	
	public void handle() {
		this.handleStageEvent();
		
		stage.show();
		/**
		 * 我突然发现好像不用这样做，因为每次判断高度，必定是页面刷新过了，那么一定可以通过判断页面的STATE是否成功来一气呵成
		 * 
		 * 比较挫的做法，在平台显示后进行调整高度布局
		 * 1. 原因是因为：当前构建和触发事件是树形的开发模型中，在显示之前就已经触发，导致部分布局不会被响应
		 * 2. 可以通过注册机制进行解决，但是注册机制太笨重了，需要好好考虑一下，目前先这样处理
		 * 
		 */
		/*
		content.getEditor().updateScrollHeight();
		content.getOutput().updateScrollHeight();
		*/
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void handleStageEvent() {
		// TODO Auto-generated method stub
		ChangeListener listener = new UpdateScrollHeightHandler(content);
		stage.heightProperty().addListener(listener);
		stage.widthProperty().addListener(listener);
		stage.maximizedProperty().addListener(listener);
	}
	
}
